import axios from 'axios'
import { Message } from 'element-ui'
import { Loading } from 'element-ui';
import router from '../../router'
// let  apiBaseUrl = 'http://192.168.1.13:81/index.php'
// let  apiBaseUrl = 'http://www.henanmaotong.com/index.php'
let  apiBaseUrl = 'https://yl.lizuzhe.com'
// let  apiBaseUrl = 'http://www.qrcode.com'

export function get(url, params = {}) {
	 url = apiBaseUrl+url
	 var authorization = sessionStorage.getItem('token')
	 
	 axios.defaults.headers = {
		 authorization
	 }
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params
      })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}
export function post(url, params = {}) {
	 url = apiBaseUrl+url
	 
	 
	 const _loading = Loading.service({
	   text: '加载中',
	   background: 'rgba(255, 255, 255, 0.1)',
	   spinner: 'el-icon-loading',
	   customClass: 'loadingclass'
	 })
	 var authorization = sessionStorage.getItem('token')
	 if(authorization){
		 axios.defaults.headers = {
		 	"Authorization":authorization
		 }
	 }
	
  return new Promise((resolve, reject) => {
    axios
      .post(url, params)
      .then(res => {
        const result = res.data
       _loading.close()
	   if(result.code==401){
		   sessionStorage.removeItem('token')
		   router.push({path:'/'})
	   }else{
		   resolve(result)
	   }
        
      })
      .catch(err => {
        if (err && err.response) {
           _loading.close()
          // reject(err.response)
        }
      })
  })
}

//上传图片
export const uploadImage = (e) => {
	const _loading = Loading.service({
	  text: '加载中',
	  background: 'rgba(255, 255, 255, 0.1)',
	  spinner: 'el-icon-loading',
	  customClass: 'loadingclass'
	})
	let  url = apiBaseUrl + '/api/upqcloudcos'
	var formdata = new FormData();
	formdata.append("file", e.file);
  return new Promise((resolve, reject) => {
    axios
      .post(url, formdata, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then(res => {
        resolve(res.data)
		 _loading.close()
      })
      .catch(err => {
        reject(err.data)
		 _loading.close()
      })
  })
}

//上传zip文件
export const uploadZip = (url, data) => {
  return new Promise((resolve, reject) => {
    axios
      .post(url, data, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}



// 登陆
export const login = p => post(apiBaseUrl + '/tszg-auth-core/admin/security/login', p)
//查询所有角色
export const getRoleList = p => post(apiBaseUrl + '/tszg-auth-core/admin/role/manager/page', p)


